package com.zh.analysis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.utils.RedisUtil;
import com.zh.common.websocket.ThmsWebSocketHandler;
import com.zh.common.websocket.ThmsWebSocketMessage;
import com.zh.device.dao.MattressDao;
import com.zh.device.domain.BedVo;
import com.zh.device.domain.MattressVO;
import com.zh.device.po.DevcieExample;
import com.zh.device.po.DeviceRela;
import com.zh.device.service.BehaviorService;
import com.zh.analysis.service.MattressService;

import com.zh.device.util.DeviceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 床垫分析逻辑处理类
 */
@Service
public class MattressServiceImpl implements MattressService {

    private Logger logger = LoggerFactory.getLogger(MattressServiceImpl.class);

    @Autowired
    private MattressDao mattressDao;

    @Autowired
    private BehaviorService behaviorService;

    /**
     * 床垫分析程序
     *
     * @param jsonObject
     */
    @Override
    public void proxyMattress(JSONObject jsonObject) {

        String deviceSn = jsonObject.getString("sn");
        JSONObject jsonData = jsonObject.getJSONObject("d");
        boolean wt = jsonData.getBoolean("wt");
        DevcieExample devcieExample = (DevcieExample) RedisUtil.get(deviceSn);
        if (null == devcieExample) return;

        //step1 尿湿检测
        //todo 尿湿 调用推送报警
        this.wetness(wt, deviceSn, devcieExample, jsonObject);

        //step2 睡眠心态忽高乎低检测
        this.heartbeat(deviceSn, devcieExample, jsonObject);


        //step3 推送webSocket
        this.sendWebSocketMessage(deviceSn,devcieExample,jsonObject);
    }

    /**
     * 推送床垫的websocket信息
     * @param deviceSn
     * @param devcieExample
     * @param jsonObject
     */
    private void sendWebSocketMessage(String deviceSn, DevcieExample devcieExample, JSONObject jsonObject) {
        List<DeviceRela> deviceRelas = devcieExample.getDeviceRelas();
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        for (DeviceRela deviceRela : deviceRelas) {
            twsm = new ThmsWebSocketMessage("SING", deviceRela.getDeviceBindingId(), "sign", jsonObject);
            //发送
            swsh.sendMessageToUsers(twsm);
        }
    }


    /**
     * 心率和心跳忽高忽低监测
     * 床垫3秒传一次
     * 监测为逻辑为 没30秒监测一次 获取心率和心态的波动率
     *
     * @param deviceSn
     * @param devcieExample
     * @param jsonObject
     */
    private void heartbeat(String deviceSn, DevcieExample devcieExample, JSONObject jsonObject) {
        //todo 需要通过波动率算法来实现

    }


    /**
     * 尿湿处理
     * 有尿湿就记录
     *
     * @param wt
     * @param deviceSn
     */
    private void wetness(boolean wt, String deviceSn, DevcieExample devcieExample, JSONObject jsonObject) {
        if (wt) {
            try {
                devcieExample.getBehaviorDO().setEquipmentId(deviceSn);
                devcieExample.getBehaviorDO().setStartTime(DeviceUtil.formatCreateByDate(jsonObject.getString("timeCreate")));
                devcieExample.getBehaviorDO().setRemark(devcieExample.getBehaviorDO().getFamilyUserName() + "床垫编号:" + deviceSn + "于" + DeviceUtil.formatCreateDate(jsonObject.getString("timeCreate")) + "发生尿湿请注意");
                devcieExample.getBehaviorDO().setBehaviorType(3);
                devcieExample.getBehaviorDO().setAnomalies(1);//行为异常
                behaviorService.save(devcieExample.getBehaviorDO());

                //todo 推送尿湿信息
            } catch (Exception e) {

            }
        }
    }


    @Override
    public List<BedVo> findBedByProtectLevelId(Map<String, Object> map) {
        return null;
    }

    /**
     * 床垫json数据转化为对象
     *
     * @param jsonObject
     * @return
     */
    private MattressVO jsonToMattressVO(JSONObject jsonObject) {
        MattressVO mattressVO = new MattressVO();

        return mattressVO;
    }
}
